package org.project.task.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.project.task.common.R;
import org.project.task.entity.Admin;
import org.project.task.entity.Project;
import org.project.task.common.Search;
import org.project.task.service.AdminService;
import org.project.task.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/project")
public class ProjectController extends BaseController {
    @Autowired
    private ProjectService projectService;
    @Autowired
    private AdminService adminService;

    @PostMapping("/getList")
    public R<Page<Project>> getList(@RequestBody Search search) {
        try {
            Page<Project> pageData = projectService.page(
                    getPage(),
                    new QueryWrapper<Project>()
                            .like(StrUtil.isNotBlank(
                                            search.getContent()),
                                    "description",
                                    search.getContent()
                            )
                            .or()
                            .like(StrUtil.isNotBlank(
                                            search.getContent()),
                                    "name",
                                    search.getContent()
                            )
                            .orderByDesc("update_time")
            );

            for (Project project : pageData.getRecords()) {
                Admin admin = adminService.getById(project.getAdminId());
                project.setAdmin(admin);
            }
            return new R<Page<Project>>().success(pageData);
        } catch (Exception e) {
            return new R<Page<Project>>().error(e.getMessage());
        }
    }

    @GetMapping("/projectList")
    public R<List<Project>> projectList() {
        R<List<Project>> result = new R<>();
        List<Project> projectList = projectService.list();
        if (projectList != null && !projectList.isEmpty()) {
            result.success(projectList);
        } else {
            result.error("查询对象不存在!");
        }
        return result;
    }


    @GetMapping("/getById/{id}")
    public R<Project> getById(@PathVariable("id") Integer id) {
        R<Project> result = new R<>();
        Project project = projectService.getById(id);
        if (project != null) {
            Admin admin = adminService.getById(project.getAdminId());
            project.setAdmin(admin);
            result.success(project);
        } else {
            result.error("查询对象不存在!");
        }
        return result;
    }
    @PostMapping("/delete")
    public R<Project> delete(@RequestBody List<Long> ids) {
        R<Project> result = new R<>();
        if(projectService.removeByIds(ids)){
            result.success("删除成功！");
        } else {
            result.error("删除失败！");
        }
        return result;
    }

    @PostMapping("/insert")
    public R<Project> insert(@RequestBody Project project) {
        R<Project> result = new R<>();
        if (project != null) {
            project.setCreateTime(new Date());
            project.setUpdateTime(new Date());
            boolean rsg = projectService.save(project);
            if (rsg) {
                result.setData(project);
                result.success("添加成功！");
            } else {
                result.error("添加失败！");
            }
        } else {
            result.error("请传入正确参数！");
        }
        return result;
    }

    @PostMapping("/update")
    public R<Project> update(@RequestBody Project project) {
        R<Project> result = new R<>();
        if (project != null) {
            project.setUpdateTime(new Date());
            boolean rsg = projectService.updateById(project);
            if (rsg) {
                result.setData(project);
                result.success("修改成功！");
            } else {
                result.error("修改失败！");
            }
        } else {
            result.error("请传入正确参数！");
        }
        return result;
    }
}
